<?php
class Unit
{
	public static function safe($data) 
	{
		if ( ! get_magic_quotes_gpc()) {
			return addslashes($data);
		}
		return $data;
	}
	
	public static function ishere($router, $type = 2)
	{
		$res = false;
		if ( ! empty($router)) {
			switch ($type) {
				case 0:
					$ruri = $_SERVER['REQUEST_URI'];
					if ($ruri == $router) $res = true;
				break;
				case 1:
					if ('/' . Router::init()->getParam(0) == $router) $res = true;
				break;
				case 2:
					if ('/' . Router::init()->getParam(0) . '/' . Router::init()->getParam(1) == $router) $res = true;
				break;
			}
			
		}
		
		return $res;
	}
	
	public static function generateToken($params = array())
	{
		$result = '';
        $ary = array();
		if (is_array($params) && count($params)) {
            foreach ($params as $k => $v) {
                $tmp = $k . '=' . $v;
                $ary[] = $tmp;
            }
			$signStr = implode('&', $ary);
			$result = strtoupper(md5(md5($signStr)));
		}
		return $result;
	}
	
	public static function generateSecret($params = array())
	{
		$result = '';
		$ary = array();
		if (is_array($params) && count($params)) {
		    foreach ($params as $k => $v) {
		        $tmp = $k . '=' . $v;
                $ary[] = $tmp;
            }
            $ary['time'] = date('y-m-d', time());
			$signStr = implode('&', $ary);
			$result = substr(md5(md5($signStr)), 0, 16);
		}
		return $result;
	}
	
	public static function validateToken($token, $params)
	{
		$result = false;
		$token_string = $token;
		$token_params = self::generateToken($params);
		if ($token_string === $token_params) $result = true;
		return $result;
	}
}